using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

public class PlayerController : MonoBehaviour, IBeAttack
{
	public StateType_Player 当前状态_仅显示;

	[Header("键位")]
	[HideInInspector]
	public const KeyCode _Key_交互 = KeyCode.E;

	[HideInInspector]
	public const KeyCode _Key_饮酒 = KeyCode.R;

	[HideInInspector]
	public const KeyCode _Key_定身 = KeyCode.U;

	[HideInInspector]
	public const KeyCode _Key_跳跃 = KeyCode.I;

	[HideInInspector]
	public const KeyCode _Key_棍花 = KeyCode.O;

	[HideInInspector]
	public const KeyCode _Key_轻棍 = KeyCode.J;

	[HideInInspector]
	public const KeyCode _Key_重棍 = KeyCode.K;

	[HideInInspector]
	public const KeyCode _Key_翻滚 = KeyCode.L;

	[Header("音效")]
	public AudioClip _AudioC_轻击;

	public AudioClip _AudioC_重击;

	public AudioClip _AudioC_定身;

	public AudioClip _AudioC_蓄力时;

	public AudioClip _AudioC_蓄力加豆子时;

	public AudioClip _AudioC_喝药时;

	public AudioClip _AudioC_升级;

	public AudioClip _AudioC_跳跃;

	public AudioClip _AudioC_完美闪避1;

	public AudioClip _AudioC_完美闪避2;

	public AudioClip _AudioC_死亡;

	[Header("------------")]
	[HideInInspector]
	public Rigidbody2D _Rb2D;

	[HideInInspector]
	public Transform _Tsf_Body;

	[HideInInspector]
	public Animator _Ani;

	[HideInInspector]
	public float _CurrHeight;

	[HideInInspector]
	public C_攻击判断 _C_攻击判断;

	[HideInInspector]
	public Transform _Tsf_Model;

	public GameObject _GO_完美闪避残影Prefab;

	public GameObject _GO_蓄力特效;

	private FSM<StateType_Player, PlayerStateBase> _FSM;

	public Dictionary<string, float> _Dict_AniName_time;

	public bool _朝向右边 = true;

	private Vector2 _mDir;

	[SerializeField]
	private float _定身术CD = 20f;

	public SpriteRenderer _SR_Model;

	public SpriteRenderer _SR_影子;

	[HideInInspector]
	public bool _Is死亡;

	[HideInInspector]
	public ParticleSystem _PS_识破特效;

	public I定身术 _定身目标;

	private bool _mIsOk_定身术 = true;

	private Coroutine _Cor_设置定身术CD;

	[HideInInspector]
	public bool _Is免疫远程;

	public StateType_Player _State_攻击连段_判定 = StateType_Player.轻棍1;

	public float _State_攻击连段_判定Timer;

	[Header("玩家相关属性,测试可见")]
	[SerializeField]
	public int _mMax等级;

	[SerializeField]
	private int _mCurr等级;

	[SerializeField]
	private float _mMax生命;

	[SerializeField]
	private float _mCurr生命;

	[SerializeField]
	private float _mMax气力;

	[SerializeField]
	private float _mCurr气力;

	[SerializeField]
	private float _气力回转timer;

	[SerializeField]
	private float _m攻击力;

	[SerializeField]
	private float _mMax蓄力;

	[SerializeField]
	private float _mCurr蓄力;

	private int _mCurr豆子;

	[SerializeField]
	public float _mMax下一级所需经验;

	[SerializeField]
	private float _mCurr经验;

	[SerializeField]
	private int _mMax酒;

	[SerializeField]
	private int _mCurr酒;

	[SerializeField]
	private float _m葫芦_数量;

	[SerializeField]
	private float _m葫芦_恢复;

	[SerializeField]
	private float _m棍势_一段;

	[SerializeField]
	private float _m棍势_二段;

	[SerializeField]
	private float _m棍势_三段;

	[SerializeField]
	private float _m气力_恢复速度;

	[SerializeField]
	private float _m气力_回转时间;

	public int _释放豆子;

	public bool _触发完美闪避;

	public bool _可以受击 = true;

	public bool _释放蓄力状态;

	public bool _可以识破;

	public bool _isOk_定身术
	{
		get
		{
			return _mIsOk_定身术;
		}
		set
		{
			_mIsOk_定身术 = value;
			if (!_mIsOk_定身术)
			{
				MonoSingleton<GameManager>.Instance.F_UI_定身术CD(_定身术CD);
				F_设置定身术CD(_定身术CD);
			}
		}
	}

	public float _蓄力额外速度 { get; set; }

	public Vector2 _BeAttack_Pos { get; set; }

	public float _BeAttack_击退力度 { get; set; }

	public int _阵营
	{
		get
		{
			return 1;
		}
		set
		{
			Debug.LogError("WTF??????");
		}
	}

	public Vector2 _Dir => _mDir;

	public float _攻击力 => _m攻击力 * (float)((!GameManager._10倍攻击) ? 1 : 10);

	public float _Max生命 => _mMax生命;

	public float _葫芦_恢复 => _m葫芦_恢复;

	public int _Curr等级
	{
		get
		{
			return _mCurr等级;
		}
		set
		{
			_mCurr等级 = Mathf.Clamp(value, 0, _mMax等级);
			MonoSingleton<MyDataManager>.Instance._玩家等级 = _mCurr等级;
			F_初始化属性(游戏中升级: true);
			this._On_Curr等级_Change(_mCurr等级);
			MonoSingleton<AudioManager>.Instance.F_Play_SFX(_AudioC_升级);
		}
	}

	public int _Curr酒
	{
		get
		{
			if (GameManager._无限酒)
			{
				return _mMax酒;
			}
			return _mCurr酒;
		}
		set
		{
			_mCurr酒 = value;
			_mCurr酒 = Mathf.Clamp(_mCurr酒, 0, _mMax酒);
			this._On_Curr酒_Change(_mCurr酒, _mMax酒);
		}
	}

	public float _Curr生命
	{
		get
		{
			return _mCurr生命;
		}
		set
		{
			if (GameManager._无敌)
			{
				value = _mMax生命;
			}
			_mCurr生命 = value;
			_mCurr生命 = Mathf.Clamp(_mCurr生命, -1E-45f, _mMax生命);
			this._On_Curr生命_Change(_mCurr生命, _mMax生命);
		}
	}

	public float _Curr气力
	{
		get
		{
			return _mCurr气力;
		}
		set
		{
			if (GameManager._无限气力)
			{
				value = _mMax气力;
			}
			_mCurr气力 = value;
			_气力回转timer = _m气力_回转时间;
			_mCurr气力 = Mathf.Clamp(_mCurr气力, -1E-45f, _mMax气力);
			this._On_Curr气力_Change(_mCurr气力, _mMax气力);
		}
	}

	public float _Curr蓄力
	{
		get
		{
			return _mCurr蓄力;
		}
		set
		{
			_mCurr蓄力 = value;
			_mCurr蓄力 = Mathf.Clamp(_mCurr蓄力, -1E-45f, _mMax蓄力);
			if (_mCurr蓄力 + float.Epsilon >= _mMax蓄力 && _Curr豆子 != F_获取当前豆子上限())
			{
				_Curr豆子++;
				if (_Curr豆子 != F_获取当前豆子上限())
				{
					_mCurr蓄力 = 0f;
				}
			}
			this._On_Curr蓄力_Change(_mCurr蓄力, _mMax蓄力);
		}
	}

	public int _Curr豆子
	{
		get
		{
			if (GameManager._无限豆子)
			{
				return 2;
			}
			return _mCurr豆子;
		}
		set
		{
			if (_mCurr豆子 != value)
			{
				_mCurr豆子 = value;
				int num = F_获取当前豆子上限();
				_mCurr豆子 = Mathf.Clamp(_mCurr豆子, 0, num);
				if (_mCurr豆子 == 0)
				{
					_mMax蓄力 = _m棍势_一段;
				}
				else if (num > 1 && _mCurr豆子 == 1)
				{
					_mMax蓄力 = _m棍势_二段;
				}
				else if (num > 2 && _mCurr豆子 == 2)
				{
					_mMax蓄力 = _m棍势_三段;
				}
				this._On_Curr豆子_Change(_mCurr豆子);
			}
		}
	}

	public event Action<int> _On_Curr等级_Change = delegate
	{
	};

	public event Action<float, float> _On_Curr生命_Change = delegate
	{
	};

	public event Action<float, float> _On_Curr气力_Change = delegate
	{
	};

	public event Action<float, float> _On_Curr蓄力_Change = delegate
	{
	};

	public event Action<int> _On_Curr豆子_Change = delegate
	{
	};

	public event Action<float, float, float> _On_Curr经验_Change = delegate
	{
	};

	public event Action<float, float> _On_Curr酒_Change = delegate
	{
	};

	public event Action _On_Die = delegate
	{
	};

	public void F_Ani_Paly_AudioC_轻击()
	{
		MonoSingleton<AudioManager>.Instance.F_Play_SFX(_AudioC_轻击);
	}

	public void F_Ani_Paly_AudioC_重击()
	{
		MonoSingleton<AudioManager>.Instance.F_Play_SFX(_AudioC_重击);
	}

	public void F_设置定身术CD(float time)
	{
		if (_Cor_设置定身术CD != null)
		{
			MonoSingleton<GameManager>.Instance.StopCoroutine(_Cor_设置定身术CD);
		}
		_Cor_设置定身术CD = MonoSingleton<GameManager>.Instance.StartCoroutine(Cor_设置定身术CD(time));
	}

	public IEnumerator Cor_设置定身术CD(float time)
	{
		yield return new WaitForSeconds(time);
		_Cor_设置定身术CD = null;
		_mIsOk_定身术 = true;
	}

	private int F_获取当前豆子上限()
	{
		if (_Curr等级 >= 6)
		{
			return 3;
		}
		if (_Curr等级 >= 3)
		{
			return 2;
		}
		return 1;
	}

	public void F_增加经验(float val)
	{
		if (_Curr等级 != _mMax等级)
		{
			_mCurr经验 += val;
			if (_mCurr经验 >= _mMax下一级所需经验)
			{
				Debug.LogError("======升级======");
				_mCurr经验 -= _mMax下一级所需经验;
				_Curr等级++;
			}
			this._On_Curr经验_Change(_mCurr经验, _mMax下一级所需经验, val);
			MonoSingleton<MyDataManager>.Instance._PlayerData._Player经验 = _mCurr经验;
			MonoSingleton<MyDataManager>.Instance.F_保存数据();
		}
	}

	private void Awake()
	{
		F_初始化组件();
		F_初始化属性();
		F_初始化状态();
		MonoSingleton<MySceneManager>.Instance._event_触发切换场景 += delegate
		{
			_Rb2D.velocity = Vector2.zero;
		};
	}

	private void F_初始化组件()
	{
		_Rb2D = GetComponent<Rigidbody2D>();
		_Ani = GetComponent<Animator>();
		_PS_识破特效 = base.transform.Find("识破特效").GetComponent<ParticleSystem>();
		_C_攻击判断 = base.transform.Find("攻击判断1").GetComponent<C_攻击判断>();
		_Tsf_Body = base.transform.Find("Body");
		_Tsf_Model = base.transform.Find("Model");
		_Dict_AniName_time = new Dictionary<string, float>();
		_FSM = new FSM<StateType_Player, PlayerStateBase>();
		_GO_蓄力特效.GetComponent<Player_蓄力特效>().F_Init(this);
		_C_攻击判断._阵营 = _阵营;
	}

	public void F_初始化属性(bool 游戏中升级 = false)
	{
		SO_Data_Player data_玩家基础 = MonoSingleton<MyDataManager>.Instance._Data_玩家基础;
		_mMax等级 = data_玩家基础._List_等级对应数值.Count - 1;
		_mCurr等级 = MonoSingleton<MyDataManager>.Instance._玩家等级;
		_mCurr等级 = Mathf.Clamp(_mCurr等级, 0, _mMax等级);
		_mMax酒 = data_玩家基础._葫芦_数量;
		_mCurr酒 = _mMax酒;
		_m葫芦_恢复 = data_玩家基础._葫芦_恢复;
		_m棍势_一段 = data_玩家基础._棍势_一段;
		_m棍势_二段 = data_玩家基础._棍势_二段;
		_m棍势_三段 = data_玩家基础._棍势_三段;
		_m气力_恢复速度 = data_玩家基础._气力_恢复速度;
		_m气力_回转时间 = data_玩家基础._气力_回转时间;
		ItemSO_Data_Player_等级数值 itemSO_Data_Player_等级数值 = data_玩家基础._List_等级对应数值[_mCurr等级];
		_mMax生命 = itemSO_Data_Player_等级数值._生命 * GameManager._已三连;
		_mCurr生命 = _mMax生命;
		_mMax气力 = itemSO_Data_Player_等级数值._气力;
		_mCurr气力 = _mMax气力;
		_m攻击力 = itemSO_Data_Player_等级数值._攻击力;
		_蓄力额外速度 = itemSO_Data_Player_等级数值._蓄力额外速度;
		_mMax下一级所需经验 = itemSO_Data_Player_等级数值._下一级所需经验;
		_mMax蓄力 = _m棍势_一段;
		_mCurr蓄力 = 0f;
		_Curr酒 = _Curr酒;
		if (!游戏中升级)
		{
			_Curr蓄力 = 0f;
			_Curr豆子 = 0;
			_mCurr经验 = MonoSingleton<MyDataManager>.Instance._PlayerData._Player经验;
			Debug.LogError($"_Player经验<<{MonoSingleton<MyDataManager>.Instance._PlayerData._Player经验}");
			Debug.LogError($"_Player经验<<{_mCurr经验}");
		}
		_Curr生命 = _Curr生命;
		_Curr气力 = _Curr气力;
		this._On_Curr酒_Change(_Curr酒, _Curr酒);
		this._On_Curr生命_Change(_mCurr生命, _mMax生命);
		this._On_Curr气力_Change(_mCurr气力, _mMax气力);
		this._On_Curr蓄力_Change(_mCurr蓄力, _mMax蓄力);
		this._On_Curr豆子_Change(_mCurr豆子);
		this._On_Curr等级_Change(_mCurr等级);
		this._On_Curr蓄力_Change(_mCurr蓄力, _mMax蓄力);
		this._On_Curr经验_Change(_mCurr经验, _mMax下一级所需经验, -1f);
	}

	private void F_初始化状态()
	{
		AnimationClip[] animationClips = _Ani.runtimeAnimatorController.animationClips;
		foreach (AnimationClip animationClip in animationClips)
		{
			_Dict_AniName_time.TryAdd(animationClip.name, animationClip.length);
			Debug.Log($"{animationClip.name}=={animationClip.length}");
		}
		_FSM.states = new Dictionary<StateType_Player, PlayerStateBase>
		{
			{
				StateType_Player.正常移动,
				new PlayerState_正常移动().Init(this)
			},
			{
				StateType_Player.轻棍1,
				new PlayerState_轻棍1().Init(this)
			},
			{
				StateType_Player.轻棍2,
				new PlayerState_轻棍2().Init(this)
			},
			{
				StateType_Player.轻棍3,
				new PlayerState_轻棍3().Init(this)
			},
			{
				StateType_Player.棍花,
				new PlayerState_棍花().Init(this)
			},
			{
				StateType_Player.破棍式,
				new PlayerState_破棍式().Init(this)
			},
			{
				StateType_Player.斩棍式,
				new PlayerState_斩棍式().Init(this)
			},
			{
				StateType_Player.跳跃,
				new PlayerState_跳跃().Init(this)
			},
			{
				StateType_Player.受击,
				new PlayerState_受击().Init(this)
			},
			{
				StateType_Player.跳跃重击,
				new PlayerState_跳跃重击().Init(this)
			},
			{
				StateType_Player.翻滚,
				new PlayerState_翻滚().Init(this)
			},
			{
				StateType_Player.完美闪避,
				new PlayerState_完美闪避().Init(this)
			},
			{
				StateType_Player.蓄力,
				new PlayerState_蓄力().Init(this)
			},
			{
				StateType_Player.释放蓄力,
				new PlayerState_释放蓄力().Init(this)
			},
			{
				StateType_Player.喝酒,
				new PlayerState_喝酒().Init(this)
			},
			{
				StateType_Player.定身术,
				new PlayerState_定身术().Init(this)
			},
			{
				StateType_Player.死亡,
				new PlayerState_死亡().Init(this)
			},
			{
				StateType_Player.打坐,
				new PlayerState_打坐().Init(this)
			}
		};
		_FSM.OnStateChange += delegate(StateType_Player a, StateType_Player b)
		{
			当前状态_仅显示 = b;
		};
		ChangeState(StateType_Player.正常移动);
	}

	public void F_打坐(Vector2 pos)
	{
		base.transform.position = pos;
		ChangeToState(StateType_Player.打坐);
	}

	public void ChangeToState(StateType_Player state)
	{
		_FSM.ChangeState(state);
	}

	public void ChangeState(StateType_Player state)
	{
		if (_FSM.states.TryGetValue(state, out var value))
		{
			if (_Curr气力 < value._Data_Player招式_系数._气力消耗)
			{
				Debug.LogError("气力不足，无法切换:" + state);
				_FSM.ChangeState(StateType_Player.正常移动);
			}
			else
			{
				_FSM.ChangeState(state);
			}
		}
		else
		{
			Debug.LogError("无法切换到:" + state);
			_FSM.ChangeState(StateType_Player.正常移动);
		}
	}

	private void Update()
	{
		if (_Is死亡 || GameManager._暂停游戏 || !GameManager._CanPlay)
		{
			return;
		}
		if (_State_攻击连段_判定Timer > 0f)
		{
			_State_攻击连段_判定Timer -= Time.deltaTime;
			if (_State_攻击连段_判定Timer <= 0f)
			{
				_State_攻击连段_判定 = StateType_Player.轻棍1;
			}
		}
		_FSM.OnUpdate();
		if (_气力回转timer <= 0f)
		{
			if (_mCurr气力 < _mMax气力)
			{
				_mCurr气力 += Time.deltaTime * _m气力_恢复速度;
				_mCurr气力 = Mathf.Clamp(_mCurr气力, 0f, _mMax气力);
				this._On_Curr气力_Change(_mCurr气力, _mMax气力);
			}
		}
		else
		{
			_气力回转timer -= Time.deltaTime;
        }
        int y = (int)(Mathf.Abs((gameObject.transform.position.y - 10) * 5));
        GetComponent<SortingGroup>().sortingOrder = y;
    }

	public bool F_PlayerMove(float speed, bool can转向 = true)
	{
		float horizontal = InputContrl.Horizontal;
		float num = InputContrl.Vertical / 2f;
		_mDir = new Vector2(horizontal, num).normalized;
		_Rb2D.velocity = Vector2.Lerp(_Rb2D.velocity, speed * _mDir, Time.deltaTime * 20f);
		if (can转向)
		{
			F_玩家转向(horizontal);
		}
		if (horizontal == 0f && num == 0f)
		{
			return false;
		}
		return true;
	}

	public void F_玩家转向(float x)
	{
		if (x > 0f && !_朝向右边)
		{
			_朝向右边 = true;
			base.transform.localScale = new Vector3(1f, 1f, 1f);
		}
		else if (x < 0f && _朝向右边)
		{
			_朝向右边 = false;
			base.transform.localScale = new Vector3(-1f, 1f, 1f);
		}
	}

	public void BeAttack(float atk_生命, float atk_僵直, float atk_击退力度, Vector2 atk_Pos, bool atk_Is远程 = false)
	{
		if (_Is死亡 || GameManager._暂停游戏 || !GameManager._CanPlay)
		{
			return;
		}
		if (_Is免疫远程 && atk_Is远程)
		{
			_Curr气力 -= 5f;
			return;
		}
		_BeAttack_Pos = atk_Pos;
		_BeAttack_击退力度 = atk_击退力度;
		if (_可以识破)
		{
			_PS_识破特效.Play();
			_Curr豆子++;
		}
		else if (_触发完美闪避)
		{
			ChangeState(StateType_Player.完美闪避);
			_Curr豆子++;
		}
		else
		{
			if (!_可以受击)
			{
				return;
			}
			if (_释放蓄力状态)
			{
				_Curr生命 -= atk_生命 * 0.4f;
				if (_Curr生命 <= float.Epsilon)
				{
					ChangeState(StateType_Player.死亡);
					this._On_Die();
				}
			}
			else
			{
				_Curr生命 -= atk_生命;
				if (_Curr生命 > -1E-45f)
				{
					ChangeState(StateType_Player.受击);
					return;
				}
				ChangeState(StateType_Player.死亡);
				this._On_Die();
			}
		}
	}

	public void F_喝酒()
	{
		if (!_Is死亡 && !GameManager._暂停游戏 && GameManager._CanPlay)
		{
			_FSM.currState.F_喝酒();
		}
	}

	public void F_棍花Down()
	{
		if (!_Is死亡 && !GameManager._暂停游戏 && GameManager._CanPlay)
		{
			_FSM.currState.F_棍花Down();
		}
	}

	public void F_棍花Up()
	{
		if (!_Is死亡 && !GameManager._暂停游戏 && GameManager._CanPlay)
		{
			_FSM.currState.F_棍花Up();
		}
	}

	public void F_跳跃()
	{
		if (!_Is死亡 && !GameManager._暂停游戏 && GameManager._CanPlay)
		{
			_FSM.currState.F_跳跃();
		}
	}

	public void F_轻攻击()
	{
		if (!_Is死亡 && !GameManager._暂停游戏 && GameManager._CanPlay)
		{
			_FSM.currState.F_轻攻击();
		}
	}

	public void F_重攻击Down()
	{
		if (!_Is死亡 && !GameManager._暂停游戏 && GameManager._CanPlay)
		{
			_FSM.currState.F_重攻击Down();
		}
	}

	public void F_重攻击Up()
	{
		if (!_Is死亡 && !GameManager._暂停游戏 && GameManager._CanPlay)
		{
			_FSM.currState.F_重攻击Up();
		}
	}

	public void F_翻滚()
	{
		if (!_Is死亡 && !GameManager._暂停游戏 && GameManager._CanPlay)
		{
			_FSM.currState.F_翻滚();
		}
	}

	public void F_定身术()
	{
		if (!_Is死亡 && !GameManager._暂停游戏 && GameManager._CanPlay)
		{
			_FSM.currState.F_定身术();
		}
	}
}
